﻿@{
    ViewBag.Title = "WMS入库任务";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var statuses = ViewBag.InboundTaskStatuses as List<KeyValuePair<int, string>>;
    var startTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddDays(-1).AddHours(20) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(8) : DateTime.Today.AddHours(20));
    var endTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddHours(8) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(20) : DateTime.Today.AddDays(1).AddHours(8));
}

@section Contents{
    <link href="~/res/kendo/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="~/res/kendo/styles/kendo.material.min.css" rel="stylesheet" />
    <style>
        .layui-form-label {
            width: auto;
        }

        #txtOrderNo, #txtWorkOrderNo {
            width: 100%;
            height: 30px;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <form class="layui-form layui-card-header layuiadmin-card-header-auto" method="post" action="/Report/ExportComponents">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">车间：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode" @(workshops.IndexOf(item) == 0 ? " selected " : "")>@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.InboundTime">入库时间：</label>
                        <div class="layui-input-inline" style="width:440px;">
                            <input type="text" id="txtRangeTime" style="width:310px;float:left;" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input">
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnPreDuty" type="button" style="margin-top:-6px;">
                                <i class="layui-icon layui-icon-prev layuiadmin-button-btn"></i>
                            </button>
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnNextDuty" type="button" style="margin-top:-6px;margin-left:0;">
                                <i class="layui-icon layui-icon-next layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.InboundStatus">入库状态：</label>
                        <div class="layui-input-inline">
                            <select id="selStatuses" lay-filter="selStatuses">
                                <option value="">全部</option>
                                @foreach (var item in statuses)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">订单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtOrderNo" name="orderNo" value="" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtWorkOrderNo" name="workOrderNo" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CabinetNo">柜号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtCabinetNo" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.PackNo">箱号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtPackNo" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" style="margin-top:-6px;" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </form>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script src="/res/kendo/kendo.all.min.js"></script>
    <script src="/res/kendo/js/cultures/kendo.culture.zh-CN.min.js"></script>
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    laydate = layui.laydate,
                    table = layui.table,
                    form = layui.form;
                laydate.render({
                    elem: '#txtRangeTime'
                    , type: 'datetime'
                    , range: '至'
                    , done: function (value, date, endDate) {
                        queryNos(0);
                        queryNos(1);
                    }
                });
                form.on("select(selWorkshops)", function (data) {
                    queryNos(0);
                    queryNos(1);
                });
                var txtOrderNos = $("#txtOrderNo").kendoAutoComplete({
                    dataSource: vm.$data.nos,
                    filter: "contains",
                    placeholder: "选择订单号"
                }).focus(function () {
                    txtOrderNos.search("");
                }).data("kendoAutoComplete"),
                    txtWorkOrderNos = $("#txtWorkOrderNo").kendoAutoComplete({
                        dataSource: vm.$data.nos,
                        filter: "contains",
                        placeholder: "选择工单号"
                    }).focus(function () {
                        txtWorkOrderNos.search("");
                    }).data("kendoAutoComplete");
                function queryNos(noType) {
                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    var startTime = rangeTime.length <= 0 ? null : rangeTime[0],
                        endTime = rangeTime.length <= 0 ? null : rangeTime[1];
                    $.ajax({
                        url: "/Common/QueryOrderNos?noType=" + noType + "&workshopCode=" + $("#selWorkshops").val().trim() + "&key=" + "&startTime=" + startTime + "&endTime=" + endTime,
                        type: "GET",
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            if (noType == 0) {
                                txtOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            } else {
                                txtWorkOrderNos.setDataSource(new kendo.data.DataSource({
                                    data: res.Data
                                }));
                            }
                        }
                    });
                }

                var options = {
                    elem: '#table'
                    , url: '/Wms/QueryWmsInboundTasks'
                    , method: "post"
                    , where: { workshopCode: "", status: null, startTime: "", endTime: "", orderNo: "", workOrderNo: "", cabinetNo: "", packNo: "" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: "left" }
                        , { field: 'InboundNo', title: '入库单号', fixed: "left", width: 150 }
                        , { field: 'WorkshopName', title: '生产车间', minWidth: 100 }
                        , { field: 'OrderNo', title: '订单号', minWidth: 150 }
                        , { field: 'WorkOrderNo', title: '工单号', minWidth: 150 }
                        , { field: 'CabinetNo', title: '柜号', minWidth: 150 }
                        , { field: 'CabinetTimeStr', title: '拼柜时间', minWidth: 170 }
                        , { field: 'PackNo', title: '打包编号', minWidth: 180 }
                        , { field: 'PackTimeStr', title: '打包时间', minWidth: 170 }
                        , {
                            title: '入库状态', minWidth: 100, templet: function (d) {
                                var color = d.Status == 0 ? "#666" : (d.Status == 1 ? "#009688" : "#FF5722");
                                return '<span style="color:' + color + ';">' + d.CnStatus + '</span>';
                            }
                        }
                        , { field: 'LastTryTimeStr', title: '最后入库时间', minWidth: 170 }
                        , { field: 'RetryTimes', title: '尝试次数', minWidth: 100 }
                        , {
                            title: '收货状态', minWidth: 100, templet: function (d) {
                                var color = d.Status == 0 ? "#666" : (d.Status == 2 ? "#009688" : "#FF5722");
                                return '<span style="color:' + color + ';">' + d.CnReceiveStatus + '</span>';
                            }
                        }
                        , { field: 'ReceiveTimeStr', title: '收货时间', minWidth: 170 }
                        , { field: 'Remark', title: '备注', minWidth: 100 }
                        , {
                            title: '操作', width: 200, fixed: "right", templet: function (d) {
                                var btns = '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="detail(\'' + d.InboundNo + '\')"><i class="layui-icon">&#xe614;</i>入库明细</button>';
                                if (d.Status == 2) {
                                    btns = btns + '<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="retry(\'' + d.InboundNo + '\')"><i class="layui-icon">&#xe669;</i>重试</button>';
                                }
                                return btns;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.workshopCode = $("#selWorkshops").val().trim();
                    options.where.status = $("#selStatuses").val().trim();
                    options.where.orderNo = $("#txtOrderNo").val().trim();
                    options.where.workOrderNo = $("#txtWorkOrderNo").val().trim();
                    options.where.cabinetNo = $("#txtCabinetNo").val().trim();
                    options.where.packNo = $("#txtPackNo").val().trim();

                    var rangeTime = $("#txtRangeTime").val().trim() == '' ? new Array() : $("#txtRangeTime").val().trim().split(' 至 ');
                    options.where.startTime = rangeTime.length <= 0 ? null : rangeTime[0];
                    options.where.endTime = rangeTime.length <= 0 ? null : rangeTime[1];

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function detail(inboundNo) {
            parent.layui.index.openTabsPage("/Wms/WmsInboundTaskItems?inboundNo=" + inboundNo, "任务明细(" + inboundNo + ")");
        }

        function retry(inboundNo) {
            var loadId = layer.load();
            $.ajax({
                url: "/Wms/RetryWmsInboundTask",
                data: {
                    inboundNo: inboundNo
                },
                complete: function () {
                    layer.close(loadId);
                },
                success: function (res) {
                    if (res.ErrCode != 200) {
                        layer.msg(res.ErrMsg, { icon: 5 });
                        return;
                    }
                }
            });
        }
    </script>
}
